'use strict';

import {createStore, applyMiddleware, combineReducers} from 'redux';
import thunkMiddleware from 'redux-thunk';
import {
    createReactNavigationReduxMiddleware,
} from 'react-navigation-redux-helpers';

import mainmodel from '../page/main2/reducer';
import order from '../page/order/reducer';
import login from '../page/login/reducer';
import user from '../page/my/reducer';
import register from '../page/register/reducer';
import income from '../page/income/reducer';//收入
import product from '../page/product/reducer';//商品
import ticket from '../page/ticket/reducer';
import shop from '../page/shop/reducer';
import request from '../commonActions/requestReducer';

import AppNavigator from '../navigatorApp/appNavigator';
import app from '../navigatorApp/reducer'

const initialState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Home'));

const navReducer = (state = initialState, action) => {
  const nextState = AppNavigator.router.getStateForAction(action, state);

  // Simply return the original `state` if `nextState` is null or undefined.
  return nextState || state;
};

const middleware = createReactNavigationReduxMiddleware(
	"root",
	state => state.nav,
);

const rootReducer = combineReducers({
    mainmodel,
    order,
    login,
    user,
    register,
    income,
    product,
    request,
    ticket,
    shop,
    app,
    nav: navReducer,
})

const store = createStore(
    rootReducer,
    applyMiddleware(middleware),
    applyMiddleware(thunkMiddleware)
);

export default store;

// const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore);
// export default function configureStore(initialState) {
//     const store = createStoreWithMiddleware(rootReducer, initialState);
//     return store;
// }